package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;
	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<JSONObject> categories1 = new ArrayList<>();
		// 查询所有的分类
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(null);
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return null;
		}
		// 根据一级分类id对分类进行分组，获取一级分类数据Map<一级分类id, 同一个一级分类id下的数据集合>
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		category1Map.forEach((category1Id, category2List) -> {
			// 每一个kv转化成一级分类的json对象
			JSONObject jsonObject1 = new JSONObject();
			// 把一级分类json对象放入分类集合
			categories1.add(jsonObject1);
			// 设置一级分类id
			jsonObject1.put("categoryId", category1Id);
			// 设置一级分类name：由于category1List是根据具体数据进行分组的，所以该分组数据不可能为空，否则不会有该分组数据
			jsonObject1.put("categoryName", category2List.get(0).getCategory1Name());
			// 设置一级分类下的二级分类
			List<JSONObject> categories2 = new ArrayList<>();
			jsonObject1.put("categoryChild", categories2);
			// 根据二级分类id对同一个一级分类下的数据进行分组，获取二级分类数据Map<二级分类id, 同一个二级分类id下的数据集合>
			Map<Long, List<BaseCategoryView>> category2Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			category2Map.forEach((category2Id, category3List) -> {
				// 把每一个kv转化成二级分类的json对象
				JSONObject jsonObject2 = new JSONObject();
				categories2.add(jsonObject2);
				jsonObject2.put("categoryId", category2Id);
				jsonObject2.put("categoryName", category3List.get(0).getCategory2Name());
				// 设置二级分类下的三级分类
				// 循环遍历二级分类下的数据集合，转化成三级分类json对象集合
				jsonObject2.put("categoryChild", category3List.stream().map(baseCategoryView -> {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					return jsonObject3;
				}).collect(Collectors.toList()));
			});
		});
		return categories1;
	}

	@Override
	public List<BaseAttribute> findAttributeByCategory1Id(Long category1Id) {
		//	调用mapper层方法
		return baseAttributeMapper.selectBaseAttributeList(category1Id);
	}

	@Override
	public BaseCategoryView queryCategoryView(Long category3Id) {
		return this.baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//根据一级分类查询二级分类
		List<BaseCategory2> baseCategory2s = this.baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategory2s)){
			return null;
		}
		List<Long> category2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());

		//根据二级分类id集合查询三级分类
		return this.baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2Ids)
				.eq(BaseCategory3::getIsTop,1)
				.last("limit 7"));
		}

	@Override
	public JSONObject getBaseCategory(Long category1Id) {
		//初始化一个JSONObjectd(一级分类对象)
		JSONObject jsonObject1 = new JSONObject();
		//根据一级分类id查询出所有的二级和三级分类
		List<BaseCategoryView> baseCategoryViews = this.baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return jsonObject1;
		}

		//给一级分类设置id和name
		jsonObject1.put("categoryId",category1Id);
		//通过该分组数据中的第一条数据获取一级分类的名称,有改组数据的情况下,不可能为空
		jsonObject1.put("categoryName",baseCategoryViews.get(0).getCategory1Name());

		//设置二级分类集合
		List<JSONObject> jsonObjects2 = new ArrayList<>();
		jsonObject1.put("categoryChild",jsonObjects2);

		//给二级分类数据组装数据
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//遍历每一个kv组装出对应的二级分类
		category2Map.forEach((category2Id, category3s) -> {
			JSONObject jsonObject2 = new JSONObject();
			jsonObjects2.add(jsonObject2);
			// 给二级分类设置数据
			jsonObject2.put("categoryId", category2Id);
			jsonObject2.put("categoryName", category3s.get(0).getCategory2Name());
			// 把三级分类组装出来
			jsonObject2.put("categoryChild", category3s.stream().map(category3 -> {
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId", category3.getCategory3Id());
				jsonObject3.put("categoryName", category3.getCategory3Name());
				return jsonObject3;
			}).collect(Collectors.toList()));
		});
		return jsonObject1;
	}
}